package com.opensky.qatest.ui.component;

import org.openqa.selenium.By;

import com.opensky.qatest.ui.page.CuratorPickerPage;
import com.opensky.qatest.ui.page.thirdparty.FacebookLoginPage;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * SignUp form on Home page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class HomePageSignUpForm {

    // ************************ Locators ************************

    private static final String REGFORM = "//div[@class='reg-form']/form";

    private static final String CATEGORIES = "//div[@class='category-follow']";

    private static final String FACEBOOK_LINK = "//a[contains(., 'Log In With Facebook')]";

    private static final String TERMS_LINK = "//div[@class='terms']/label[contains(., 'terms')]";

    private static final String NAME_FIELD = "registration_user_firstName";

    private static final String EMAIL_FIELD = "registration_user_email";

    private static final String PASSWORD_FIELD = "registration_user_plainPassword";

    private static final String START_SHOPPING_BTN = "submit";

    private static final String INVALID_EMAIL_ALERT = "//label[@for='registration_user_email']/span[contains(., 'Please provide valid email address.')]";
    
    private static final String INVALID_PASSWORD_ALERT = "//label[@for='registration_user_plainPassword']/span[contains(., 'Please provide a password. ')]";
    
    private static final String SHORT_PASSWORD_ALERT = "//label[@for='registration_user_plainPassword']/span[contains(., 'Your password is too short. It must have 6 characters or more.')]";
    

    private final WebDriverWrapper driver;

    public HomePageSignUpForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    public FacebookLoginPage clickLogInWithFacebookLink() {
	driver.switchTo().defaultContent();
	driver.waitAndClick("FACEBOOK_LINK", By.xpath(FACEBOOK_LINK));
	return new FacebookLoginPage(driver);
    }

    public CuratorPickerPage signUp(String name, String email, String password) {
	
	CuratorPickerPage curatorPickerPage = new CuratorPickerPage(driver);
	
	driver.waitAndSendKeys("NAME_FIELD", By.id(NAME_FIELD), name);
	driver.waitAndSendKeys("EMAIL_FIELD", By.id(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.id(PASSWORD_FIELD), password);
	
	driver.waitAndClick("START_SHOPPING_BTN", By.name(START_SHOPPING_BTN));
	
	if(driver.waitTimeoutAndCheckElementPresent("INVALID_EMAIL_ALERT", By.xpath(INVALID_EMAIL_ALERT), 3) || 
	   driver.waitTimeoutAndCheckElementPresent("INVALID_PASSWORD_ALERT", By.xpath(INVALID_PASSWORD_ALERT), 3) || 
	   driver.waitTimeoutAndCheckElementPresent("SHORT_PASSWORD_ALERT", By.xpath(SHORT_PASSWORD_ALERT), 3)) {
	    return null;
	}
	
	if(curatorPickerPage.isPageLoaded()) {
	    return curatorPickerPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    public boolean isSignUpFormPresent() {

	driver.waitAndCheckElementPresent("REGFORM", By.xpath(REGFORM));
	return true;
    }

    public boolean isBaseElementsPresent() {

	driver.waitAndCheckElementPresent("NAME_FIELD", By.id(NAME_FIELD));
	driver.waitAndCheckElementPresent("EMAIL_FIELD", By.id(EMAIL_FIELD));
	driver.waitAndCheckElementPresent("PASSWORD_FIELD", By.id(PASSWORD_FIELD));
	driver.waitAndCheckElementPresent("CATEGORIES", By.xpath(CATEGORIES));
	driver.waitAndCheckElementPresent("FACEBOOK_LINK", By.xpath(FACEBOOK_LINK));
	driver.waitAndCheckElementPresent("START_SHOPPING_BTN", By.name(START_SHOPPING_BTN));
	driver.waitAndCheckElementPresent("TERMS_LINK", By.xpath(TERMS_LINK));

	return true;
    }

    public boolean isInvalidEmailAlertPresent() {

	driver.waitAndCheckElementPresent("INVALID_EMAIL_ALERT", By.xpath(INVALID_EMAIL_ALERT));
	return true;
    }

    public boolean isEmptyPasswordAlertPresent() {

	driver.waitAndCheckElementPresent("INVALID_PASSWORD_ALERT", By.xpath(INVALID_PASSWORD_ALERT));
	return true;
    }
    
    public boolean isShortPasswordAlertPresent() {

	driver.waitAndCheckElementPresent("SHORT_PASSWORD_ALERT", By.xpath(SHORT_PASSWORD_ALERT));
	return true;
    }
}
